package com.catmiw.wyz;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.commons.lang.time.DateUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellRangeAddress;


public class Trainning {
	public static String STRING="0123456789";
	public static Random rand=new Random();
	
	public static String getRandomString(int digit){
		StringBuilder temp=new StringBuilder();
		
		for (int i=0;i<digit;i++){
			temp.append(STRING.charAt(rand.nextInt(10)));
		}
		
		return temp.toString();
	}
	
	public static double PAGEWIDTH=22000;
	public static double RATIO=0.4;
	
	public static void writeTable(Sheet sheet,int r,int c,int count,CellStyle cs,short rowheight){
		List<Integer> list=new ArrayList<Integer>();
		for (int i=1;i<=count*count;i++){
			list.add(i);
		}
		for (int i=0;i<count;i++){
			for (int j=0;j<count;j++){
				int t=rand.nextInt(list.size());
				int row=r*(count+1)+i+3;
				int col=c*(count+1)+j;
				Row srow=sheet.getRow(row);
				if (srow==null)
					srow=sheet.createRow(row);
				Cell cell=srow.getCell(col);
				if (cell==null)
					cell=srow.createCell(col);
				cell.setCellStyle(cs);
				cell.setCellValue(list.get(t));
				list.remove(t);
			}
		}
	}
	
	public static void test3(){
		Workbook wb = new HSSFWorkbook();
		Sheet sheet = wb.createSheet();
		for (int i=0;i<5;i++){
			sheet.setColumnWidth(i*2, 1100);
			sheet.setColumnWidth(i*2+1, 3500);
		}
	    CellStyle cs=wb.createCellStyle();
		Font font=wb.createFont();
		font.setFontHeightInPoints((short)14);
		font.setFontName("Arial");
	    cs.setFont(font);
	    cs.setBorderBottom((short)1);
	    cs.setBorderTop((short)1);
	    cs.setBorderLeft((short)1);
	    cs.setBorderRight((short)1);
	    
	    CellStyle cs2=wb.createCellStyle();
		Font font2=wb.createFont();
		font2.setFontHeightInPoints((short)14);
		font2.setFontName("Arial");
	    cs2.setFont(font2);
	    cs2.setVerticalAlignment( CellStyle.VERTICAL_CENTER);

	    Date date =new Date();
	    int rownum=0;
	    for (int j=0;j<2;j++){
	    	for (int i=0;i<10;i++){
	    		writeDate(date,6+j,sheet,cs,cs2,rownum);
	    		date=DateUtils.addDays(date, 1);
	    		rownum+=7;
	    	}
	    }
		OutputStream os = null;
		try {
			os = new FileOutputStream("D:/����׿�Ľ���/��ͥ����ƻ�/������д20110726.xls");
			
		} catch (FileNotFoundException e1) {
			e1.printStackTrace();
			return ;
		}
			
		try {
			wb.write(os);
			os.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static void writeDate(Date date,int digit,Sheet sheet,CellStyle cs,CellStyle cs2,int rownum){
		Row row = sheet.createRow(rownum);
		row.setHeight((short)500);
		sheet.addMergedRegion(new CellRangeAddress(rownum,rownum,0,9));
		Cell cell=row.createCell(0);
		cell.setCellValue("���ڣ�"+DateFormatUtils.format(date, "yyyy-MM-dd"));
		cell.setCellStyle(cs2);
		for (int i=rownum+1;i<=rownum+6;i++){
			row=sheet.createRow(i);
			row.setHeight((short)500);
			for (int j=0;j<5;j++){
				cell=row.createCell(j*2);
				cell.setCellStyle(cs);
				cell.setCellValue((i-rownum-1)*5+j+1);
				cell=row.createCell(j*2+1);
				cell.setCellStyle(cs);
				cell.setCellType(cell.CELL_TYPE_STRING);
				cell.setCellValue(getRandomString(digit));
			}
		}
	}
	
	public static void test2(){
		Workbook wb = new HSSFWorkbook();
		Date date=new Date();
		
		for (int i=6;i<=10;i++){
		    CellStyle cs=wb.createCellStyle();
		    cs.setBorderBottom((short)1);
		    cs.setBorderTop((short)1);
		    cs.setBorderLeft((short)1);
		    cs.setBorderRight((short)1);
		    cs.setAlignment(CellStyle.ALIGN_CENTER);
		    cs.setVerticalAlignment( CellStyle.VERTICAL_CENTER);
		    
		    CellStyle cs2=wb.createCellStyle();
		    cs2.setAlignment(CellStyle.ALIGN_LEFT);
		    cs2.setVerticalAlignment( CellStyle.VERTICAL_CENTER);
			Font font=wb.createFont();
			font.setFontHeightInPoints((short)(100/i+1));
			System.out.println(font.getFontHeightInPoints());
		    cs.setFont(font);
		    cs2.setFont(font);
			for (int j=0;j<10;j++){
				
				Sheet sheet=wb.createSheet(DateFormatUtils.format(date, "yyyy-MM-dd"));
				createSheet(sheet,i,cs,cs2);
				date=DateUtils.addDays(date, 1);
			}
		}
		
		OutputStream os = null;
		try {
			os = new FileOutputStream("D:/����׿�Ľ���/��ͥ����ƻ�/˳��������20110726.xls");
			
		} catch (FileNotFoundException e1) {
			e1.printStackTrace();
			return ;
		}
			
		try {
			wb.write(os);
			os.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static void createSheet(Sheet sheet,int count, CellStyle cs, CellStyle cs2){
		short colwidth=(short)(PAGEWIDTH/(count*2+1));
		short rowheight=(short)(RATIO*PAGEWIDTH/(count*2+1));
		
		for (int i=0;i<2*count+4;i++){
			sheet.setColumnWidth(i, colwidth);
			Row row=sheet.createRow(i);
			row.setHeight(rowheight);
		}
		sheet.addMergedRegion(new CellRangeAddress (0,0,0,2*count));
		sheet.addMergedRegion(new CellRangeAddress(1,1,0,2*count));
		
		Cell cell=sheet.getRow(0).createCell(0);
		cell.setCellValue("���ڣ�"+sheet.getSheetName());
		cell.setCellStyle(cs2);
		cell=sheet.getRow(1).createCell(0);
		cell.setCellValue("��ʱ��");
		cell.setCellStyle(cs2);
		
	    for (int i=0;i<2;i++){
	    	for (int j=0;j<2;j++){
	    		writeTable(sheet,i,j,count,cs,rowheight);
	    	}
	    }
	}
	
	public static String PM="+-";
	public static String getRandomExpression(int dif1,int dif2){
		int n1=rand.nextInt(100);
		char p1=PM.charAt(rand.nextInt(2));
		char p2=PM.charAt(rand.nextInt(2));
		int n2=0;
		int n3=0;
		int r1=0;
		int r2=0;
		if (p1=='+'){
			int tn1=100-n1;
			n2=rand.nextInt((dif1==0)?((tn1<10)?tn1:10):(tn1+1));
			r1=n1+n2;
		} else {
			System.out.println(n1);
			n2=rand.nextInt((dif1==0)?((n1<10)?(n1+1):10):(n1+1));
			r1=n1-n2;
		}
		if (dif2==0){
			return ""+n1+" "+p1+" "+n2+" "+" = "+r1;
		}
		if (p2=='+'){
			int tn1=100-r1;
			n3=rand.nextInt((dif1==0)?((tn1<10)?tn1:10):(tn1+1));
			r2=r1+n3;
		} else {
			n3=rand.nextInt((dif1==0)?((r1<10)?(r1+1):10):(r1+1));
			r2=r1-n3;
		}
		
		return ""+n1+" "+p1+" "+n2+" "+p2+" "+n3+" "+" = "+r2;
	}
	
	public static void writeDate4(Date date,int dif1,int dif2,Sheet sheet,CellStyle cs,CellStyle cs2,int rownum){
		Row row = sheet.createRow(rownum);
		row.setHeight((short)700);
		sheet.addMergedRegion(new CellRangeAddress(rownum,rownum,0,5));
		Cell cell=row.createCell(0);
		cell.setCellValue("���ڣ�"+DateFormatUtils.format(date, "yyyy-MM-dd"));
		cell.setCellStyle(cs2);
		for (int i=rownum+1;i<=rownum+5;i++){
			row=sheet.createRow(i);
			row.setHeight((short)500);
			for (int j=0;j<3;j++){
				cell=row.createCell(j*2);
				cell.setCellStyle(cs);
				cell.setCellValue((i-rownum-1)*3+j+1);
				cell=row.createCell(j*2+1);
				cell.setCellStyle(cs);
				cell.setCellType(Cell.CELL_TYPE_STRING);
				cell.setCellValue(getRandomExpression(dif1,dif2));
			}
		}
	
	}
	/**
	 * 100���ڵļӼ���������������
	 * @param args
	 */
	public static void test4(){
		Workbook wb = new HSSFWorkbook();
		Sheet sheet = wb.createSheet();
		for (int i=0;i<3;i++){
			sheet.setColumnWidth(i*2, 1500);
			sheet.setColumnWidth(i*2+1, 6200);
		}
	    CellStyle cs=wb.createCellStyle();
		Font font=wb.createFont();
		font.setFontHeightInPoints((short)14);
		font.setFontName("Arial");
	    cs.setFont(font);
	    cs.setBorderBottom((short)1);
	    cs.setBorderTop((short)1);
	    cs.setBorderLeft((short)1);
	    cs.setBorderRight((short)1);
	    cs.setAlignment(CellStyle.ALIGN_CENTER);
	    
	    CellStyle cs2=wb.createCellStyle();
		Font font2=wb.createFont();
		font2.setFontHeightInPoints((short)14);
		font2.setFontName("Arial");
	    cs2.setFont(font2);
	    cs2.setVerticalAlignment( CellStyle.VERTICAL_CENTER);

	    Date date =new Date();
	    int rownum=0;
	    for (int j=0;j<2;j++){
	    	for (int k=0;k<2;k++){
	    		for (int i=0;i<10;i++){
	    			writeDate4(date,k,j,sheet,cs,cs2,rownum);
	    			date=DateUtils.addDays(date, 1);
	    			rownum+=6;
	    		}
	    	}
	    }
		OutputStream os = null;
		try {
			os = new FileOutputStream("D:/����׿�Ľ���/��ͥ����ƻ�/�Ӽ�20110727.xls");
			
		} catch (FileNotFoundException e1) {
			e1.printStackTrace();
			return ;
		}
			
		try {
			wb.write(os);
			os.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
	/**
	 * �����������=10
	 */
	public static void test5(){
		Workbook wb = new HSSFWorkbook();
		Sheet sheet = wb.createSheet();
		sheet.setColumnWidth(0, 24000);
		
	    CellStyle cs=wb.createCellStyle();
		Font font=wb.createFont();
		font.setFontHeightInPoints((short)14);
		font.setFontName("Arial");
		cs.setVerticalAlignment(CellStyle. VERTICAL_CENTER);
	    cs.setFont(font);
	    cs.setBorderBottom((short)1);
	    cs.setBorderTop((short)1);
	    cs.setBorderLeft((short)1);
	    cs.setBorderRight((short)1);
	    cs.setWrapText(true);

	    Date date =new Date();
	    
	    int rownum=0;
	    
	    for (int i=0;i<30;i++){
	    	Row row=sheet.createRow(rownum);
	    	row.setHeight((short)600);
	    	rownum++;
	    	Cell cell=row.createCell(0);
	    	cell.setCellStyle(cs);
	    	cell.setCellValue("���ڣ�"+DateFormatUtils.format(date, "yyyy-MM-dd"));
	    	row=sheet.createRow(rownum);
	    	row.setHeight((short)600);
	    	rownum++;
	    	cell=row.createCell(0);
	    	cell.setCellStyle(cs);
	    	cell.setCellValue("��ʱ��                                    ����");
	    	row=sheet.createRow(rownum);
	    	row.setHeight((short)2000);
	    	rownum++;
	    	cell=row.createCell(0);
	    	cell.setCellStyle(cs);
	    	StringBuilder sb=new StringBuilder();
	    	for (int j=0;j<210;j++){
	    		sb.append(rand.nextInt(10));
	    		sb.append(" ");
	    	}
	    	cell.setCellValue(sb.toString());
	    	date=DateUtils.addDays(date,1);
	    }

		OutputStream os = null;
		try {
			os = new FileOutputStream("D:/����׿�Ľ���/��ͥ����ƻ�/�������ֵ���10-20110801.xls");
			
		} catch (FileNotFoundException e1) {
			e1.printStackTrace();
			return ;
		}
			
		try {
			wb.write(os);
			os.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args) {
		test5();

	}

}
